//
//  SearchViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 7/18/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchCell.h"
@interface SearchViewController ()

@end

@implementation SearchViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.leftBtn.hidden =YES;
    
    [self initSearchViews];
    [self initTableViews];
}

#pragma mark 创建搜索框UI
-(void)initSearchViews
{
    CGFloat aheight =[[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f?20:0;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,aheight, 320, 44)];
    view.backgroundColor = [UIColor clearColor];
    [self.navigationBarView addSubview:view];
    
    UIImageView * searchBgView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 7.5f, 304-45, 58/2.0f)];
    searchBgView.image = [UIImage imageNamed:@"searchFrame"];
    searchBgView.userInteractionEnabled = YES;
    [view addSubview:searchBgView];

    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(7, 7.5, 13, 13)];
    imageView.image = [UIImage imageNamed:@"search_10"];
    [searchBgView addSubview:imageView];
    
    self.searchTF = [[UITextField alloc] initWithFrame:CGRectMake(26,0, 530/2.0f-30,searchBgView.frame.size.height)];
    _searchTF.placeholder = @"请输入关键词";
    _searchTF.delegate = self;
    _searchTF.backgroundColor = [UIColor clearColor];
    _searchTF.returnKeyType = UIReturnKeySearch;
    _searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _searchTF.textAlignment = UIControlContentVerticalAlignmentCenter;
    _searchTF.font = [UIFont systemFontOfSize:14];
    [_searchTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [searchBgView addSubview:_searchTF];
    [self performSelector:@selector(searchTFBecomeFirstResponder) withObject:nil afterDelay:0.001];
    UIButton * cancelButton = [UIButton initButton:CGRectMake(width_x(searchBgView)+10,0, UI_SCREEN_WIDTH-(width_x(searchBgView)+10), view.frame.size.height) btnImage:nil btnTitle:@"取消"];
    [cancelButton addTarget:self action:@selector(cancelSearch:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:cancelButton];
    
}

-(void)searchTFBecomeFirstResponder
{
    [_searchTF becomeFirstResponder];
}

#pragma mark 创建TableView
-(void)initTableViews
{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, self.contentView.frame.size.height) style:UITableViewStylePlain];
    _myTableView.backgroundColor = [UIColor clearColor];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.showsVerticalScrollIndicator = NO;
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:_myTableView];
    
    tableFootView = [UIView initViewRect:CGRectMake(0, 0, UI_SCREEN_WIDTH, 60) andBackGroudColor:[UIColor clearColor]];
    UIButton * clearButton = [UIButton initButton:CGRectMake(10, 10, 300, 40) btnImage:nil btnTitle:@"清空搜索记录"andTitleColor:[UIColor blackColor]];
    clearButton.layer.masksToBounds=YES;
    clearButton.layer.cornerRadius=3.0f;
    clearButton.backgroundColor = [UIColor lightGrayColor];
    [clearButton addTarget:self action:@selector(celarHistory:) forControlEvents:UIControlEventTouchUpInside];
    [tableFootView addSubview:clearButton];
    _myTableView.tableFooterView = tableFootView;
}

#pragma mark UITextField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_searchTF resignFirstResponder];
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

-(void)textFieldDidChange:(UITextField *)textField
{
    
}

#pragma mark UITableView Delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName = @"cell";
    SearchCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil){
        cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
    }
    cell.titleLB.text =[NSString stringWithFormat:@"清除历史记录%d",indexPath.row];
    return cell;
}

#pragma mark UIScrollView Delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_searchTF resignFirstResponder];
}


#pragma mark UIButton Selector
-(void)cancelSearch:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)celarHistory:(UIButton *)button
{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
